package ru.ognick.option;

import ru.ognick.locale.ILocale;
import ru.ognick.locale.Locale;
import ru.ognick.logic.Praxis;
import android.graphics.Color;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class LayotBuilder {
	final static LayoutParams WIDTH_FILL_PARENT = new LayoutParams(
			LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
	final private EditorActivity own;
	private final ILocale L = Locale.get();
	public Button cur_media_button = null;
	
	LayotBuilder(EditorActivity own) {
		this.own = own;
	}
	
	public LinearLayout createLayout(Praxis praxis) {
		LinearLayout praxisLayoutParent = new LinearLayout(own);
		praxisLayoutParent.setOrientation(LinearLayout.VERTICAL);
		praxisLayoutParent.setBackgroundColor(Color.GRAY);

		final LinearLayout praxisLayout = new LinearLayout(own);
		praxisLayout.setOrientation(LinearLayout.VERTICAL);
		
		final LinearLayout accordionLayout = new LinearLayout(own);
		accordionLayout.setOrientation(LinearLayout.HORIZONTAL);
		
		Button upButton = new Button(own);
		upButton.setTag("UP_BUTTON");
		upButton.setText(L.G_BUTTON_UP());
		accordionLayout.addView(upButton);
		
		Button downButton = new Button(own);
		downButton.setTag("DOWN_BUTTON");
		downButton.setText(L.G_BUTTON_DOWN());
		accordionLayout.addView(downButton);
		
		Button delButton = new Button(own);
		delButton.setText(L.G_BUTTON_DELETE());
		delButton.setTag("DEL_BUTTON");
		delButton.setOnClickListener(new OnClickListener() {
		
			public void onClick(View v) {
				// TODO Auto-generated method stub
			}
		});

		accordionLayout.addView(delButton);

		final Button accordion = new Button(own, null,
				android.R.attr.buttonStyleInset);
		accordion.setBackgroundColor(Color.GRAY);
		switch (praxis.getPraxisType()) {
		case action:
				accordion.setTextColor(Color.BLUE);
				accordion.setText(praxis.getTitle());			
			break;
		case passive:
				accordion.setTextColor(Color.BLACK);
				accordion.setText(L.G_PASSIVE());
			break;

		default:
				assert false;
			break;
		}
		accordionLayout.addView(accordion);
		
		praxisLayoutParent.addView(accordionLayout);

		accordion.setOnClickListener(new OnClickListener() {
		
			public void onClick(View v) {
				if (praxisLayout.getVisibility() == View.VISIBLE) {
					praxisLayout.setVisibility(View.GONE);
					accordion.setBackgroundColor(Color.GRAY);
					praxisLayout.setBackgroundColor(Color.GRAY);
				} else {
					accordion.setBackgroundColor(Color.GREEN);
					praxisLayout.setBackgroundColor(Color.GREEN);
					praxisLayout.setVisibility(View.VISIBLE);
				}
			}
		});

		//TODO:запилить единый файл тегов и названий
		OnFocusChangeListener editorListener = new OnFocusChangeListener() {
		
			public void onFocusChange(View v, boolean hasFocus) {
				if (!hasFocus)
					accordion.setText(((EditText) v).getText().toString());
			}
		};
		
		final LinearLayout title = createTextEdit("Title", praxis.getTitle(),editorListener);
		final LinearLayout content = createTextEdit("Content", praxis.getContent(),null);
		

		
		OnCheckedChangeListener radioListener = new OnCheckedChangeListener() {
		
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked)
					switch (L.G_Praxis(buttonView.getText().toString())) {
					case action:
							title.setVisibility(View.VISIBLE);
							content.setVisibility(View.VISIBLE);
							accordion.setTextColor(Color.BLUE);
							EditText editTitle = (EditText) title.findViewWithTag("TITLE");
							accordion.setText(editTitle.getText());
						break;
					case passive:
							accordion.setTextColor(Color.BLACK);
							accordion.setText("Delay");
							title.setVisibility(View.GONE);
							content.setVisibility(View.GONE);
						break;
					default:
							assert false;
						break;
					}
			}
		};
		
		praxisLayout.addView(createRadioGroup("Type" ,
				Praxis.praxisTypeStringArray(), L.G_Praxis(praxis.getPraxisType()),radioListener));
		
		praxisLayout.addView(title);
		praxisLayout.addView(content);
		praxisLayout.addView(createOptionNumber("Time",
				praxis.getTimeSpan()));
		praxisLayout.addView(createOptionNumber("PreTime",
				praxis.getTimePreload()));
		praxisLayout.addView(createOptionNumber(
				"Period speak", praxis.getPeriodSignal()));

		Button mediaButton = new Button(own);
		mediaButton.setTag("MEDIA_BUTTON");
		mediaButton.setText(praxis.getMedia());
		mediaButton.setOnClickListener(new OnClickListener() {
		
			public void onClick(View v) {
//				Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
//				intent.addCategory(Intent.CATEGORY_OPENABLE);
//				intent.setType("audio/*");
//				cur_media_button = (Button) v;
//				own.startActivityForResult(Intent.createChooser(intent, "Open media"), EditorActivity.PICK_REQUEST_CODE);
				cur_media_button = (Button) v;
				own.showDialog (EditorActivity.PICK_REQUEST_CODE) ;
			}
		});
		mediaButton.setText(praxis.getMedia());
		praxisLayout.addView(mediaButton);

		praxisLayout.setVisibility(View.GONE);
		praxisLayoutParent.addView(praxisLayout);
		
		praxisLayoutParent.setFocusable(true);
		return praxisLayoutParent;
	}
	
	public LinearLayout createRadioGroup(String label, 
			String[] captions, String defValue,OnCheckedChangeListener radioListener) {
		final LinearLayout layout = new LinearLayout(own);
		layout.setOrientation(LinearLayout.VERTICAL);
		
		TextView title = new TextView(own);
		title.setText(label + ": ");
		layout.addView(title);

		final RadioGroup radioGroup = new RadioGroup(own);
		radioGroup.setOrientation(LinearLayout.HORIZONTAL);
		layout.addView(radioGroup);
		
		int id = 1000;
		for (String caption : captions) {
			RadioButton radioButton = new RadioButton(own);
			radioButton.setText(caption);
			radioButton.setTag(caption.toUpperCase());
			radioButton.setId(id++);
			radioButton.setOnCheckedChangeListener(radioListener);
			radioButton.setChecked(caption.equals(defValue));
			radioGroup.addView(radioButton);
		}
		
		return layout;
	}
	
	public LinearLayout createTextEdit(String caption, String defValue, OnFocusChangeListener listener ){
		final LinearLayout layout = new LinearLayout(own);
		layout.setOrientation(LinearLayout.VERTICAL);
		
		TextView textTitle = new TextView(own);
		textTitle.setText(caption+": ");
		layout.addView(textTitle);
		EditText editTitle = new EditText(own);
		editTitle.setOnFocusChangeListener(listener);
		editTitle.setText(defValue);
		editTitle.setTag(caption.toUpperCase());
		editTitle.setLayoutParams(WIDTH_FILL_PARENT);
		layout.addView(editTitle);
		
		return layout;
	}
	
	public LinearLayout createOptionNumber(String caption, int defValue){
		final LinearLayout layout = new LinearLayout(own);
		layout.setOrientation(LinearLayout.VERTICAL);
		
		TextView textTimeSpan = new TextView(own);
		textTimeSpan.setText(caption+": ");
		layout.addView(textTimeSpan);

		LinearLayout timeSpanLayout = new LinearLayout(own);
		timeSpanLayout.setOrientation(LinearLayout.HORIZONTAL);

		CheckBox timeSpanCheck = new CheckBox(own);
		final EditText timeSpan = new EditText(own);
		timeSpan.setTag(caption.toUpperCase());
		timeSpan.setLayoutParams(WIDTH_FILL_PARENT);

		timeSpanCheck
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
					public void onCheckedChanged(
							CompoundButton buttonView, boolean isChecked) {
						if (isChecked) {
							timeSpan.setVisibility(View.VISIBLE);
						} else {
							timeSpan.setVisibility(View.GONE);
						}
					}
				});

		timeSpan.setRawInputType(InputType.TYPE_CLASS_NUMBER);
		if (defValue == 0) {
			timeSpanCheck.setChecked(false);
			timeSpan.setVisibility(View.GONE);
		} else {
			timeSpan.setVisibility(View.VISIBLE);
			timeSpanCheck.setChecked(true);
			timeSpan.setText(String.valueOf(defValue));
		}
		timeSpanLayout.addView(timeSpanCheck);
		timeSpanLayout.addView(timeSpan);
		
		layout.addView(timeSpanLayout);
		
		return layout;
	}

}
